<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */
class ConfigsystemController extends Controller{
    
    public function actionGrid(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        if (isset($_GET['del'])){
            $this->config_system_del = true;
            $this->validate = new TForm();
            TForm::addMessage('eliminado', "La configuracion se elimino correctamente");
        }
        else{
            $this->config_system_del = false;
        }
        
        $this->button_new_config_system = new TButton("Nuevo Configuracion");
        $this->button_new_config_system->Name = "new_config";
        
        $js_action_new_config_system = '
            $(location).attr("href","../configsystem/new");
        ';
        
        $this->button_new_config_system->onClick = $js_action_new_config_system;
        $this->button_new_config_system->Type = "button";
        $this->button_new_config_system->Width = "170";
        $this->button_new_config_system->OptionsAditional = array('style'=>array('font-weight:bold',
                                                                        'cursor:pointer',
                                                                        'border:1px solid gray',
                                                                        'background-image:url('.VeCms::getPathImage().VeCmsEnum::ICON_ADD.')',
                                                                        'background-position:left',
                                                                        'background-repeat: no-repeat',
                                                                        'background-position-x:5px',
                                                                        'text-align: right'));
        
        //cargamos el grid de las configuraciones
        
        $offset = (isset($_GET['offset']))?$_GET['offset']:0;
        
        $classConfigSystem = new config_system();
        $classConfigSystem->Limit = 10;
        $classConfigSystem->Offset = $offset;
        $configSystem = $classConfigSystem->findAll("active=:active",array(':active'=>1));
        
        $this->grid_config_system = new TTable();
        $this->grid_config_system->Id = "grid-config";
        $this->grid_config_system->Columns = 6;
        $this->grid_config_system->Rows = $classConfigSystem->Limit;
        $this->grid_config_system->Width = '100%';
        $this->grid_config_system->Headers = array("Nro","Nombre","Valor","","","");
        $this->grid_config_system->WidthCell[0] = 25;
        $this->grid_config_system->WidthCell[3] = 25;
        $this->grid_config_system->WidthCell[4] = 25;
        $this->grid_config_system->WidthCell[5] = 25;
        $this->grid_config_system->RowsAlign[0] = 'center';
        
        $contador=1;
        
        foreach ($configSystem as $key=>$value){
            
            //cargando la accion de la vista de configuracion
            
            $view = new TImage();
            $view->Src = VeCms::getPathImage().VeCmsEnum::ICON_VIEW;
            $js_view = '$(location).attr("href","./view?config_system_id='.$value->config_system_id.'");';
            $view->onClick = $js_view;
            $view->OptionsAditional['style'][] = 'cursor:pointer';
            
            $edit = new TImage();
            $edit->Src = VeCms::getPathImage().VeCmsEnum::ICON_EDIT;
            $js_edit = '$(location).attr("href","./update?config_system_id='.$value->config_system_id.'");';
            $edit->onClick = $js_edit;
            $edit->OptionsAditional['style'][] = 'cursor:pointer';
            
            $delete = new TImage();
            $delete->Src = VeCms::getPathImage().VeCmsEnum::ICON_DELETE;
            $js_delete = 'if(confirm(\'Esta seguro que desea eliminar esta configuracion?\')){
                    $(location).attr("href","./delete?config_system_id='.$value->config_system_id.'");
                }';
            $delete->onClick = $js_delete;
            $delete->OptionsAditional['style'][] = 'cursor:pointer';
            
            $this->grid_config_system->Items[] = array($contador,$value->name,$value->value,$view->show(),$edit->show(),$delete->show());
            $contador++;
        }
        
    }
    
    public function actionNew(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        $configSystem = new config_system();
        
        if (isset($_POST['guardar'])){
            $configSystem->setAttributes($_POST);
            if ($configSystem->validate()){
                if ($configSystem->save()){
                    TForm::addMessage("registrado", "La nueva configuracion se ha registrado correctamente");
                    $this->redirec(VeCms::createUrl("./grid"),5);
                }
            }
        }
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = VeCms::createUrl("./new");
        
        $this->name_label = new TLabel("Nombre:");
        $this->name_label->OptionsAditional['style'][]='font-weight:bold';
        
        $this->name_entry = new TEdit();
        $this->name_entry->Name = "name";
        $this->name_entry->Id = $this->name_entry->Name;
        $this->name_entry->Type = 'text';
        $this->name_entry->Text = $configSystem->name;
        $this->name_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->value_label = new TLabel("Valor:");
        $this->value_label->OptionsAditional['style'][]='font-weight:bold';
        
        $this->value_entry = new TEdit();
        $this->value_entry->Name = "value";
        $this->value_entry->Id = $this->name_entry->Name;
        $this->value_entry->Type = 'text';
        $this->value_entry->Text = $configSystem->value;
        $this->value_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->button_reset = new TButton("Cancelar");
        $this->button_reset->Type = "reset";
        $this->button_reset->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_entrar = new TButton("Guardar");
        $this->button_entrar->Name = "guardar";
        $this->button_entrar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
    }
    
    public function actionUpdate(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        $classConfigSystem = new config_system();
        
        if (isset($_POST['actualizar'])){
            $classConfigSystem->setAttributes($_POST);
            if ($classConfigSystem->validate()){
                if ($classConfigSystem->update()){
                    TForm::addMessage("registrado", "La nueva configuracion se ha actualizado correctamente");
                    $this->redirec(VeCms::createUrl("./grid"),5);
                }
            }
        }
        
        $configSystem = $classConfigSystem->find("config_system_id=:config_system_id",array(":config_system_id"=>$_GET['config_system_id']));
        
        $this->form = new TForm();
        $this->form->Method = 'POST';
        $this->form->Action = VeCms::createUrl("./update?config_system_id=".$_GET['config_system_id']);
        
        $this->name_label = new TLabel("Nombre:");
        $this->name_label->OptionsAditional['style'][]='font-weight:bold';
        
        $this->name_entry = new TEdit();
        $this->name_entry->Name = "name";
        $this->name_entry->Id = $this->name_entry->Name;
        $this->name_entry->Type = 'text';
        $this->name_entry->Text = $configSystem->name;
        $this->name_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->value_label = new TLabel("Valor:");
        $this->value_label->OptionsAditional['style'][]='font-weight:bold';
        
        $this->value_entry = new TEdit();
        $this->value_entry->Name = "value";
        $this->value_entry->Id = $this->name_entry->Name;
        $this->value_entry->Type = 'text';
        $this->value_entry->Text = $configSystem->value;
        $this->value_entry->OptionsAditional['style'] = array('border:1px solid black');
        
        $this->config_system_id_entry = new TEdit();
        $this->config_system_id_entry->Name = 'config_system_id';
        $this->config_system_id_entry->Id = $this->config_system_id_entry->Name;
        $this->config_system_id_entry->Text = $_GET['config_system_id'];
        $this->config_system_id_entry->IsHidden = true;
        
        $this->button_reset = new TButton("Cancelar");
        $this->button_reset->Type = "reset";
        $this->button_reset->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
        $this->button_entrar = new TButton("Actualizar");
        $this->button_entrar->Name = "actualizar";
        $this->button_entrar->OptionsAditional = array('style'=>array('font-weight:bold','cursor:pointer'));
        
    }
    
    public function actionDelete(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        if (isset($_GET['config_system_id'])){
            $configSystem = config_system::model()->find("config_system_id=:config_system_id",array(":config_system_id"=>$_GET['config_system_id']));
            $configSystem->active = 0;
            if ($configSystem->update()){
                $this->redirec("./grid",0,array("del"=>true));
            }
            else{
                throw new Exception("Error al intentar eliminar la configuracion ".$_GET['config_system_id']);
            }
        }
        else{
            throw new Exception("Error al intentar eliminar la configuracion indicada");
        }
    }
    
    public function actionView(){
        
        VeCms::AccessValidate(array("admin"=>1));
        
        if (isset($_GET['config_system_id'])){
            
            $configSystem = config_system::model()->find("config_system_id=:config_system_id",array(':config_system_id'=>$_GET['config_system_id']));
            
            
            $this->link_back = new TLink("Regresar");
            $js_link = "parent.history.back(); 
                        return false; 
                        ";
            $this->link_back->onClick = $js_link;
            $this->link_back->OptionsAditional['style'][] = "color:#A9BCF5";
            $this->link_back->OptionsAditional['style'][] = "font-weight:bold";
            
            
            $this->name_label = new TLabel("Nombre:");
            $this->name_label->OptionsAditional['style'][]='font-weight:bold';
            $this->name_value = new TLabel($configSystem->name);
            
            $this->value_label = new TLabel("Valor:");
            $this->value_label->OptionsAditional['style'][]='font-weight:bold';
            $this->value_value = new TLabel($configSystem->value);
            
        }
        else{
            throw new Exception("Error al intentar consultar la configuracion indicada");
        }
        
    }
    
}
?>
